package com.yunji.product.service.impl;

import com.yunji.common.model.cache.ProductAgentCacheDTO;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.product.bo.ProductAgentListQueryBO;
import com.yunji.product.bo.ProductAgentSelectQueryBO;
import com.yunji.product.bo.ProductAgentUpdateBO;
import com.yunji.product.cache.ProductCache;
import com.yunji.product.convert.ProductAgentConvert;
import com.yunji.product.dao.IProductAgentDao;
import com.yunji.product.dao.IProductDao;
import com.yunji.product.domain.ProductAgentDO;
import com.yunji.product.dto.ProductAgentPriceDTO;
import com.yunji.product.service.IProductAgentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Project: yunji-coffee
 * @Package com.yunji.product.service.impl
 * @FileName ProductAgentServiceImpl.java
 * @Copyright Copyright © 2024 云集互联. All Rights Reserved
 * @Company com
 * @Version 1.0
 * @Since JDK1.8.0_201
 * @CreateTime 2024-04-01 23:38:08
 * @Description 商品授权代理接口实现
 * @Modification History
 * Author					History Time			Version			Description
 * -------------------------------------------------------------------------------
 * HuaAo				2024-04-01 23:38:08		1.0				1.0
 */
@Service
public class ProductAgentServiceImpl implements IProductAgentService {

    @Resource
    private IProductAgentDao productAgentDao;

    @Resource
    private IProductDao productDao;

    /**
     * @param agent 代理商品绑定关系
     * @Title create
     * @Desc 创建代理商品绑定关系
     * @Date 2024-06-11 16:55:40.593
     * @Return boolean 创建结果
     */
    public boolean create(ProductAgentDO agent) {
        return productAgentDao.save(agent);
    }

    /**
     * @param agent 代理商品绑定关系
     * @Title update
     * @Desc 更新代理商品绑定关系
     * @Date 2024-06-11 17:43:49.704
     * @Return boolean 更新结果
     */
    @Override
    public boolean update(ProductAgentDO agent) {
        return productAgentDao.updateById(agent);
    }

    @Override
    public boolean updateByBO(ProductAgentUpdateBO bo) {
        return productAgentDao.updateByBo(bo);
    }

    /**
     * @Title select
     * @Desc 获取当前代理商品信息详情
     * @Date 2024-06-11 17:59:59.891
     * @Return ProductAgent 商品信息详情
     */
    @Override
    public ProductAgentDO select(ProductAgentSelectQueryBO bo) {
        return productAgentDao.select(bo);
    }

    /**
     * @Title search
     * @Desc 查询商品代理信息列表
     * @Date 2024-06-11 18:01:54.802
     * @Return List<ProductAgent> 商品代理信息列表
     */
    @Override
    public List<ProductAgentDO> search(ProductAgentListQueryBO bo) {
        return productAgentDao.search(bo);
    }

    /**
     * @Title search
     * @Desc 查询商品代理信息列表
     * @Date 2024-06-11 18:01:54.802
     * @Return List<ProductAgent> 商品代理信息列表
     */
    @Override
    public List<ProductAgentPriceDTO> searchDTO(ProductAgentListQueryBO bo) {
        List<ProductAgentDO> productAgentList = productAgentDao.search(bo);
        return ProductAgentConvert.domainToDTO(productAgentList, productDao);
    }


    @Override
    public List<Long> selectAgentIdByProductId(Long... ids) {
        List<String> productIds = productAgentDao.selectAgentIdByProductId(ids);
        return productIds.stream().mapToLong((t -> Long.valueOf(t))).boxed().collect(Collectors.toList());
    }

    @Override
    public boolean deleteByManagerIds(Long... managerIds) {
        return productAgentDao.deleteByManagerIds(managerIds);
    }

    @Override
    public boolean deleteByProductId(Long[] ids) {
        return productAgentDao.deleteByProductIds(ids);
    }

    @Override
    public ProductAgentCacheDTO getCacheByBelong(Long belongId, Long productId) {
        ProductAgentDO domain = productAgentDao.getCacheByBelong(belongId, productId);
        if (domain != null) {
            ProductAgentCacheDTO cache = new ProductAgentCacheDTO();
            BeanUtils.clone(domain, cache);
            return cache;
        }
        return null;
    }

    @Override
    public List<ProductAgentCacheDTO> getCacheByBelong(Long belongId, List<Long> productIds) {
        List<ProductAgentDO> list = productAgentDao.getCacheByBelong(belongId, productIds);
        if (list != null) {
            List<ProductAgentCacheDTO> dtoList = new ArrayList<>();
            for (ProductAgentDO domain : list) {
                ProductAgentCacheDTO cache = new ProductAgentCacheDTO();
                BeanUtils.clone(domain, cache);
                cache.setProductName(ProductCache.getProductName(domain.getProductId()));
                dtoList.add(cache);
            }
            return dtoList;
        }
        return Collections.emptyList();
    }
}
